var httpUtil = require('../../../../utils/httpUtil.js')
var uiUtil = require('../../../../utils/uiUtil.js')
var util = require('../../../../utils/util.js')
var emptyUtil = require('../../../templates/empty/empty.js')
const app = getApp()
var that

Page({

  /**
   * 页面的初始数据
   */
  data: {
    requestParam: { //搜索条件
      "appId": null,
      "pageNum": 0,
      "pageRows": 30,
    },
    dataList: null, //数据列表
    requestTask: null, //请求task
    enableLoadMore: true,
    isLoadingMore: false,

    emptyParam: {},

    endTime: null,
    totalSaveAmount: null,
    totalOrderAmount: null,
    sumList: null,

    total:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    uiUtil.showLoading("正在加载..")
    var appId = options.appid;
    if (!appId) { //不存在appid-退出
      wx.navigateBack({})
      return;
    }
    this.setData({
      'requestParam.appId': appId,
    })

    this.requestDataList();
    this.requestDataDetail();
  },

  /**
   * 请求月报统计数据
   */
  requestDataDetail(){
    httpUtil.PostRequestHeaderApp("summary_app_savemoney", {
      appId: that.data.requestParam.appId
    }, {
      success(res) {
        that.setData({
          endTime: res.endTime,
          totalSaveAmount: res.totalSaveAmount,
          totalOrderAmount: res.totalOrderAmount,
          sumList: res.sumList,
        })
      },
      fail(code, res) {
        uiUtil.showToast(res);
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return util.handleShareApp()
  },

  /**
   * 滑动到底部
   */
  onReachBottom: function () {
    this.requestDataList()
  },

  /**
   * 请求列表数据
   */
  requestDataList() {
    var requestTask = this.data.requestTask;
    if (requestTask) {
      try {
        requestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.requestParam.pageNum

    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }

    this.setData({
      isLoadingMore: true,
      'requestParam.pageNum': curPage + 1
    })

    var requestTask = httpUtil.PostRequestHeaderApp("search_app_savemoney", that.data.requestParam, {
      complete() {
        wx.hideLoading()
        that.setData({
          isLoadingMore: false
        })
      },
      success(res) {
        var dataList;
        if (1 == that.data.requestParam.pageNum) {
          dataList = res.list || []
          if (dataList.length == 0) { //内容为空
            emptyUtil.showEmptyView()
          } else {
            emptyUtil.hideView()
          }
        } else {
          dataList = that.data.dataList.concat(res.list || [])
        }
        that.setData({
          dataList: dataList,
          enableLoadMore: dataList.length < res.rowCount
        })
      },
      fail(code, res) {
        that.setData({
          enableLoadMore: false,
        })
        if (1 == that.data.requestParam.pageNum) { //内容为空
          var text = emptyUtil.getErrText(code, res, "获取数据失败")
          emptyUtil.showFailView(text)
        }
      }
    })
    that.setData({
      requestTask: requestTask
    })
  },

  /**
   * 月报统计下拉
   */
  totalChange(){
    that.setData({
      total: !that.data.total
    })
  }
})